package com.nexgo.payment.widget;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.nexgo.payment.util.StringUtils;

/**
 * Created by xiaox on 2017/7/7.
 */

public class ScanCodeTextWatcher implements TextWatcher {
    private EditText editText;

    public ScanCodeTextWatcher(EditText editText) {
        this.editText = editText;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String str = "";
        for (int i = 0; i < s.length(); ++i) {
            if (s.charAt(i) <= 57 && s.charAt(i) >= 48) {
                str = str + s.charAt(i);
            }
        }
        String code = StringUtils.separateWithSpace(str);
        editText.removeTextChangedListener(this);
        editText.setText(code);
        editText.setSelection(code.length());
        editText.addTextChangedListener(this);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
